package cn.chengpengper.seckill.reservation.infrastructure.repository;

import cn.chengpengper.seckill.common.code.HttpCode;
import cn.chengpengper.seckill.common.exception.SeckillException;
import cn.chengpengper.seckill.reservation.domain.model.entity.SeckillReservationConfig;
import cn.chengpengper.seckill.reservation.domain.model.entity.SeckillReservationUser;
import cn.chengpengper.seckill.reservation.domain.repository.SeckillReservationRepository;
import cn.chengpengper.seckill.reservation.infrastructure.mapper.SeckillReservationConfigMapper;
import cn.chengpengper.seckill.reservation.infrastructure.mapper.SeckillReservationUserMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author chengpeng.hu
 */
@Service
@RequiredArgsConstructor
public class SeckillReservationRepositoryImpl implements SeckillReservationRepository {

    private final SeckillReservationUserMapper seckillReservationUserMapper;

    private final SeckillReservationConfigMapper seckillReservationConfigMapper;

    @Override
    public boolean saveSeckillReservationConfig(SeckillReservationConfig seckillReservationConfig) {
        if (seckillReservationConfig == null || seckillReservationConfig.isEmpty()){
            throw new SeckillException(HttpCode.PARAMS_INVALID);
        }
        return seckillReservationConfigMapper.saveSeckillReservationConfig(seckillReservationConfig) == 1;
    }

    @Override
    public boolean updateSeckillReservationConfig(SeckillReservationConfig seckillReservationConfig) {
        if (seckillReservationConfig == null || seckillReservationConfig.isEmpty()){
            throw new SeckillException(HttpCode.PARAMS_INVALID);
        }
        return seckillReservationConfigMapper.updateSeckillReservationConfig(seckillReservationConfig) == 1;
    }

    @Override
    public boolean updateConfigStatus(Integer status, Long goodsId) {
        if (status == null || goodsId == null){
            throw new SeckillException(HttpCode.PARAMS_INVALID);
        }
        return seckillReservationConfigMapper.updateStatus(status, goodsId) == 1;
    }

    @Override
    public List<SeckillReservationConfig> getConfigList() {
        return seckillReservationConfigMapper.getConfigList();
    }

    @Override
    public SeckillReservationConfig getConfigDetail(Long goodsId) {
        if (goodsId == null){
            throw new SeckillException(HttpCode.PARAMS_INVALID);
        }
        return seckillReservationConfigMapper.getConfigDetail(goodsId);
    }

    @Override
    public List<SeckillReservationUser> getUserListByGoodsId(Long goodsId, Integer status) {
        if (goodsId == null || status == null){
            throw new SeckillException(HttpCode.PARAMS_INVALID);
        }
        return seckillReservationUserMapper.getUserListByGoodsId(goodsId, status);
    }

    @Override
    public List<SeckillReservationUser> getGoodsListByUserId(Long userId, Integer status) {
        if (userId == null || status == null){
            throw new SeckillException(HttpCode.PARAMS_INVALID);
        }
        return seckillReservationUserMapper.getGoodsListByUserId(userId, status);
    }

    @Override
    public boolean reserveGoods(SeckillReservationUser seckillReservationUser) {
        if (seckillReservationUser == null || seckillReservationUser.isEmpty()){
            throw new SeckillException(HttpCode.PARAMS_INVALID);
        }
        return seckillReservationUserMapper.reserveGoods(seckillReservationUser) == 1;
    }

    @Override
    public boolean cancelReserveGoods(Long goodsId, Long userId) {
        if (goodsId == null || userId == null){
            throw new SeckillException(HttpCode.PARAMS_INVALID);
        }
        return seckillReservationUserMapper.cancelReserveGoods(goodsId, userId) == 1;
    }

    @Override
    public SeckillReservationUser getSeckillReservationUser(Long userId, Long goodsId, Integer status) {
        if (goodsId == null || userId == null || status == null){
            throw new SeckillException(HttpCode.PARAMS_INVALID);
        }
        return seckillReservationUserMapper.getSeckillReservationUser(userId, goodsId, status);
    }
}
